{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/113_image_area.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "Add ESA Land Cover data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "dataset = ee.ImageCollection(\"ESA/WorldCover/v100\").first()\n",
    "Map.addLayer(dataset, {\"bands\": [\"Map\"]}, \"ESA Land Cover\")\n",
    "Map.add_legend(builtin_legend=\"ESA_WorldCover\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "Calculate the area of each land cover type."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = geemap.image_area_by_group(\n",
    "    dataset, scale=1000, denominator=1e6, decimal_places=4, verbose=True\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "Save the results to a CSV."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"esa_area.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "Add NLCD land cover data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "Map.add_basemap(\"HYBRID\")\n",
    "\n",
    "nlcd = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\")\n",
    "landcover = nlcd.select(\"landcover\")\n",
    "\n",
    "Map.addLayer(landcover, {}, \"NLCD Land Cover 2019\")\n",
    "Map.add_legend(\n",
    "    title=\"NLCD Land Cover Classification\", builtin_legend=\"NLCD\", height=\"465px\"\n",
    ")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "Calculate the area of each land cover type."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = geemap.image_area_by_group(\n",
    "    landcover, scale=1000, denominator=1e6, decimal_places=4, verbose=True\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13",
   "metadata": {},
   "source": [
    "Save the results to a CSV."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"nlcd_area.csv\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
